import axios from 'axios'
import { Toast } from 'vant'
import { getAuthToken } from './localstorage'
const instance = axios.create({
  baseURL: 'http://cba.itlike.com/public/index.php?s=/api/',
  timeout: 15000
})

instance.interceptors.request.use(config => {
  // 1. 配置Loading
  Toast.loading({
    message: '加载中..',
    forbidClick: true,
    loadingType: 'spinner', // 配置loading图标
    duration: 0 // 不会自动消失
  })
  // 2.配置公共请求头
  const token = getAuthToken()
  if (token) {
    config.headers.platform = 'H5'
    config.headers['Access-Token'] = token
  }
  return config
})

instance.interceptors.response.use(res => {
  const { data } = res
  if (data.status !== 200) {
    Toast(data.message)
    return Promise.reject(data.message)
  } else {
    Toast.clear()
  }
  return data
}, (err) => {
  return Promise.reject(err)
})
export default instance
